package com.simple.util.object;

import java.io.Serializable;
import java.text.MessageFormat;
import java.util.ArrayList;
import java.util.List;

import org.apache.log4j.Logger;

import com.simple.util.common.ErrorUtil;

public class JFValidationBean implements Serializable{
    /**
     * Comment for <code>serialVersionUID</code>
     */
    private static final long serialVersionUID = 4234549416690686087L;

    // NO need ValidationId in JFValidationBean, like for appexception, if validationId==VALIDATION_ID_NO_ID,then use validationMsg
    public final static String VALIDATION_ID_NO_ID = "VALIDATION_ID_NO_ID";
    
    private static final Logger LOG = Logger.getLogger(JFValidationBean.class);
    
    /** The validation id. */
	private String validationId = "";
	
	/** The validation description. */
	private String validationDescription = "";
	
	/** The param list. */
	private List<String> paramList = new ArrayList<String>();

	/**
	 * Instantiates a new jF validation bean.
	 */
	public JFValidationBean() {
	}

	/**
	 * Instantiates a new jF validation bean.
	 * 
	 * @param validationId
	 *            the validation id
	 */
	public JFValidationBean(final String validationId) {
		this(validationId, "");
	}

	/**
	 * Instantiates a new jF validation bean.
	 * 
	 * @param validationId
	 *            the validation id
	 * @param paramList
	 *            the param list
	 */
	public JFValidationBean(final String validationId, final List<String> paramList) {
		this(validationId, paramList, "");
	}

	/**
	 * Instantiates a new jF validation bean.
	 * 
	 * @param validationId
	 *            the validation id
	 * @param validationDescription
	 *            the validation description
	 */
	public JFValidationBean(final String validationId, final String validationDescription) {
		this.validationId = validationId;
		this.validationDescription = validationDescription;
	}

	/**
	 * Instantiates a new jF validation bean.
	 * 
	 * @param validationId
	 *            the validation id
	 * @param paramList
	 *            the param list
	 * @param validationDescription
	 *            the validation description
	 */
	public JFValidationBean(final String validationId, final List<String> paramList,
	        final String validationDescription) {
		this.validationId = validationId;
		this.paramList = paramList;
		this.validationDescription = validationDescription;
	}

	/**
	 * Gets the validation id.
	 * 
	 * @return the validation id
	 */
	public String getValidationId() {
		return validationId;
	}

	/**
	 * Sets the validation id.
	 * 
	 * @param validationId
	 *            the new validation id
	 */
	public void setValidationId(final String validationId) {
		this.validationId = validationId;
	}

	/**
	 * Gets the validation description.
	 * 
	 * @return the validation description
	 */
	public String getValidationDescription() {
		return validationDescription;
	}

	/**
	 * Sets the validation description.
	 * 
	 * @param validationDescription
	 *            the new validation description
	 */
	public void setValidationDescription(final String validationDescription) {
		this.validationDescription = validationDescription;
	}

	/**
	 * Gets the param list.
	 * 
	 * @return the param list
	 */
	public List<String> getParamList() {
		return paramList;
	}

	/**
	 * Sets the param list.
	 * 
	 * @param paramList
	 *            the new param list
	 */
	public void setParamList(final List<String> paramList) {
		this.paramList = paramList;
	}

	/**
	 * Build the message content based on the message pattern.
	 * 
	 * @param messagePattern
	 *            the message pattern
	 * @return the message by pattern
	 */
	public String getMessageByPattern(final String messagePattern) {
		String rtn = messagePattern;
		if (null != messagePattern) {
			rtn =  MessageFormat.format(messagePattern, paramList.toArray());
		}
		return rtn;
	}
    
    /** The parent throwable. */
    private Throwable throwable=null;
    
    private String validationMsg="";
    
    public Throwable getThrowable() {
        return throwable;
    }

    public void setThrowable(final Throwable throwable) {
        this.throwable = throwable;
        dealThrowable(throwable);
    }

    public String getValidationMsg() {
        return validationMsg;
    }

    public void setValidationMsg(final String validationMsg) {
        this.validationMsg = validationMsg;
    }

    public JFValidationBean(final String validationId, final Throwable throwable) {
    	this.validationId = validationId;
		this.validationDescription = ErrorUtil.generateStackTraceString(throwable);
        this.throwable = throwable;
        dealThrowable(throwable);
    }

    public JFValidationBean(final String validationId, final List<String> paramList,
            final Throwable throwable) {
    	this.validationId = validationId;
    	this.paramList = paramList;
		this.validationDescription = ErrorUtil.generateStackTraceString(throwable);
        this.throwable = throwable;
        dealThrowable(throwable);
    }
    
    private void dealThrowable(final Throwable throwable) {
        LOG.error(ErrorUtil.generateStackTraceString(throwable));
        //if need email to administrator, can put code here
    }
    
   
}
